/**
 * 所有的方法以"_"下划线开头
 */
const Base64 = {
	encode: function(v) {
		return window.btoa(window.encodeURIComponent(v))
	},
	decode: function(v) {
		return window.decodeURIComponent(window.atob(v))
	}
}
/* 科学计数  begin*/
const _formatNumber = function(num) {
	const numStr = String(num).replace(/,/g, '')
	const parts = numStr.split('.')
	parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')

	// 将整数部分和小数部分（如果有）重新连接起来
	const formattedNum = parts.join('.')

	return formattedNum
}
/* 编码解码字符串  begin*/
const _encodeString = function(Obj) {
	if (Obj) {
		if (Obj === '0') {
			return Obj
		}
		// 编码
		const enObj = JSON.stringify(Obj)
		return Base64.encode(enObj)
	} else {
		return Obj
	}
}
const _decodeString = function(Obj) {
	if (Obj) {
		if (Obj === '0') {
			return Obj
		}
		// 解码
		Obj = Base64.decode(Obj)
		const deObj = JSON.parse(Obj)
		return deObj
	} else {
		return Obj
	}
}

// 判断对象里是否有空
const _itemEmpty = function(obj) {
	const flag = Object.values(obj).some(v => _empty(v))
	return flag
}
// 判断是否为空
const _empty = function(obj) {
	var flag = false
	if (typeof obj !== 'boolean') {
		if (typeof obj === 'string') {
			if (obj == '') {
				flag = true
			} else if (obj == 'null') {
				flag = true
			}
		} else if (typeof obj === 'number') {
			if (obj == 0) {
				flag = true
			}
		} else if (typeof obj === 'boolean') {
			flag = obj
		} else if (typeof obj === 'undefined') {
			flag = true
		} else if (typeof obj === 'object') {
			if (obj instanceof Array) {
				if (obj.length == 0) {
					flag = true
				}
			} else if (obj instanceof Object) {
				if (Object.keys(obj).length == 0) {
					flag = true
				}
			} else if (obj == null) {
				flag = true
			}
		}
	}
	return flag
}
const _setLocalItem = function(key, value) {
	try {
		if (key === '' || key === undefined) {
			return
		}
		if (key) {
			if (value == 0) {
				value = JSON.stringify(value)
				localStorage.setItem(Base64.encode(key), value)
			}
			if (value === null || value === undefined || value === '') {
				return ''
			}
			// 编码
			const enObj = JSON.stringify(value)
			localStorage.setItem(Base64.encode(key), Base64.encode(enObj))
		}
	} catch (err) {
		console.log(err)
	}
}
const _getLocalItem = function(key) {
	try {
		if (key === null || key === '' || key === undefined) {
			return ''
		}
		if (key) {
			let value = localStorage.getItem(Base64.encode(key))
			if (value === null || value === undefined || value === '') {
				return ''
			} else {
				value = Base64.decode(value)
				return JSON.parse(value)
			}
		}
	} catch (err) {
		console.log(err)
	}
}

const _setSessionItem = function(key, value) {
	try {
		if (key === '' || key === undefined) {
			return
		}
		if (key) {
			if (value == 0) {
				value = JSON.stringify(value)
				sessionStorage.setItem(Base64.encode(key), value)
			}
			if (value === null || value === undefined || value === '') {
				return ''
			}
			// 编码
			const enObj = JSON.stringify(value)
			sessionStorage.setItem(Base64.encode(key), Base64.encode(enObj))
		}
	} catch (err) {
		console.log(err)
	}
}
const _getSessionItem = function(key) {
	if (key === null || key === '' || key === undefined) {
		return null
	}
	if (key) {
		let value = sessionStorage.getItem(Base64.encode(key))
		if (value === null || value === undefined || value === '') {
			return value
		} else {
			value = Base64.decode(value)
			return JSON.parse(value)
		}
	} else {
		return key
	}
}
const _removeLocalItem = function(key) {
	if (key === null || key === '' || key === undefined) {
		return
	}
	if (key) {
		const enObj = Base64.encode(key)
		localStorage.removeItem(enObj)
	}
}
// 清空session数据
const _removeSessionItem = function(key) {
	if (key === null || key === '' || key === undefined) {
		return
	}
	if (key) {
		const enObj = Base64.encode(key)
		sessionStorage.removeItem(enObj)
	}
}
const _isMobile = function() {
	// 判断是PC还是手机
	// let flag = navigator.userAgent.match(
	//   /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
	// )
	// ipad 处理为pc端
	const flag = navigator.userAgent.match(
		/(phone|pad|iPhone|iPod|ios|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
		)
	return flag
}
const _encode = function(Obj) {
	// 编码
	const enObj = JSON.stringify(Obj)
	// 定义zzyxt为数据key，结合解码自定义修改此key
	return {
		zzyxt: Base64.encode(enObj)
	}
}
const _decode = function(Obj) {
	// 解码
	// 判断自定义的key是否存在
	if ('zzyxt' in Obj) {
		// 使用定义zzyxt的key，获取数据可结合编码修改此key
		Obj = Base64.decode(Obj.zzyxt)
		const deObj = JSON.parse(Obj)
		return deObj
	} else {
		return ''
	}
}
// 字符串转为对象
const _parseStrObjByFor =function(strDes, delimiter) {
	let obj = {};
	if (strDes == null || strDes == '') {
		return obj;
	}
	delimiter = delimiter || ";";
	let arr = strDes.split(delimiter);
	let k, v, sub;
	for (let i = 0, len = arr.length; i < len; i++) {
		if (arr[i] !== '') {
			sub = arr[i].split("=");
			k = sub[0];
			v = sub[1];
			if (k !== '') {
				obj[k] = v;
			}
		}
	}
	return obj;

}
const _getFileName = function(data) {
	if (data) {
		const arr = data.split('/')
		const str = arr[arr.length - 1]
		return str
	} else {
		return data
	}
}
// 格式化日期
const _formDate = function(date) {
	if (date) {
		const arr = date.split('T')
		return arr && arr.length > 0 ? arr.join(' ') : ''
	} else {
		return date
	}
}
/** 数组根据数组对象中的某个属性值进行排序的方法
 * 使用例子：newArray.sort(sortBy('number',false)) //表示根据number属性降序排列;若第二个参数不传递，默认表示升序排序
 * @param attr 排序的属性 如number属性
 * @param rev true表示升序排列，false降序排序
 * */
const _sortBy = function(attr, rev) {
	// 第二个参数没有传递 默认升序排列
	if (rev == undefined) {
		rev = 1
	} else {
		rev = rev ? 1 : -1
	}

	return function(a, b) {
		a = a[attr]
		b = b[attr]
		if (a < b) {
			return rev * -1
		}
		if (a > b) {
			return rev * 1
		}
		return 0
	}
}
export {
	_setLocalItem,
	_getLocalItem,
	_isMobile,
	_encode,
	_decode,
	_parseStrObjByFor,
	_getSessionItem,
	_setSessionItem,
	_removeLocalItem,
	_removeSessionItem,
	_formatNumber,
	_encodeString,
	_decodeString,
	_itemEmpty,
	_empty,
	_getFileName,
	_formDate,
	_sortBy
}
export default {
	_setLocalItem,
	_getLocalItem,
	_isMobile,
	_encode,
	_decode,
	_parseStrObjByFor,
	_getSessionItem,
	_setSessionItem,
	_removeLocalItem,
	_removeSessionItem,
	_formatNumber,
	_encodeString,
	_decodeString,
	_itemEmpty,
	_empty,
	_getFileName,
	_formDate,
	_sortBy
}